//
//  MainViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/23.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "MainViewController.h"

#import "MainCell.h"
#import "QDViewController.h"
#import "TimeView.h"
#import "CheXianViewController.h"
#import "HBView.h"

@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource,BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>{
    HBView *hbview;
    UIView *view;
    NSTimer *time;
    BMKMapView *mapView;
    BMKLocationService *location;
    BMKGeoCodeSearch *geocodeSearch;
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self loadNav];
    [self loadUI];
    [self loadHBView];
    _tableView.hidden = YES;
    view.hidden = YES;
    [user setObject:@"http://ydd.xtangguo.com/" forKey:@"ImageUrl"];
    
    [self loadMap];
}

- (void)loadMap{
    location = [[BMKLocationService alloc] init];
    [location startUserLocationService];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [mapView viewWillAppear];
    location.delegate = self;
}

-(void)viewWillDisappear:(BOOL)animated{
    [mapView viewWillDisappear];
    location.delegate = nil;
}

-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    //停止定位
    [location stopUserLocationService];
}

-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    
    NSLog(@"%@---%@---%@--",result.addressDetail,result.address,result.businessCircle);
    
}

-(void)didFailToLocateUserWithError:(NSError *)error{
    NSLog(@"定位失败：-------------error:%@",error);
}

- (void)loadHBView{
    hbview = [[[NSBundle mainBundle] loadNibNamed:@"HBView" owner:nil options:nil] lastObject];
    hbview.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight-49);
    [self.view addSubview:hbview];
    //定时器
    time = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(sleep) userInfo:nil repeats:NO];
    
}
//定时器
- (void)sleep{
    hbview.hidden = YES;
    _tableView.hidden = NO;
    view.hidden = NO;
    [time invalidate];
    
}

- (void)loadNav{
    view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    view.backgroundColor = [UIColor whiteColor];
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:line];
    [line makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.bottom.equalTo(view.bottom);
        make.height.equalTo(0.5);
    }];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"-定位选择城市"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"-定位选择城市"] forState:UIControlStateHighlighted];
    [btn setTitle:@" 重庆市" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13.0];
    [btn addTarget:self action:@selector(selectCity) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    [btn makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(view.bottom);
        make.left.equalTo(0);
        make.width.equalTo(80);
        make.height.equalTo(40);
    }];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setImage:[UIImage imageNamed:@"签到-(2)"] forState:UIControlStateNormal];
    [btn2 setImage:[UIImage imageNamed:@"签到-(2)"] forState:UIControlStateHighlighted];
    [btn2 setTitle:@" 签到" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    btn2.titleLabel.font = [UIFont systemFontOfSize:13.0];
    [btn2 addTarget:self action:@selector(clickQD) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn2];
    [btn2 makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(view.bottom);
        make.right.equalTo(0);
        make.width.equalTo(60);
        make.height.equalTo(40);
    }];
    
}
//选择城市
- (void)selectCity{
    
}
//签到
- (void)clickQD{
    QDViewController *qd = [[QDViewController alloc] init];
    [self.navigationController pushViewController:qd animated:YES];
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-49) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 380)];
    headView.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    _tableView.tableHeaderView = headView;
    
    UILabel *lablines = [[UILabel alloc] initWithFrame:CGRectMake(0, 370, ScreenWidth, 10)];
    lablines.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [headView addSubview:lablines];
    
    //165+70+120+15
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 165)];
    imageView.image = [UIImage imageNamed:@"01002b58f0635fa8012049ef5f3226"];
    [headView addSubview:imageView];
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, imageView.frame.origin.y+imageView.frame.size.height+5, ScreenWidth, 70)];
    view1.backgroundColor = [UIColor whiteColor];
    [headView addSubview:view1];
    //中间的红线
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor redColor];
    [view1 addSubview:line];
    [line makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view1.centerX);
        make.width.equalTo(1);
        make.height.equalTo(30);
        make.top.equalTo(view1.top).offset(20);
    }];
    
    UILabel *labPeople = [[UILabel alloc] init];
    labPeople.text = @"3349";
    labPeople.textColor = [UIColor redColor];
    labPeople.font = [UIFont systemFontOfSize:20.0];
    [view1 addSubview:labPeople];
    [labPeople makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(line.left).offset(-50);
        make.top.equalTo(view1.top).offset(8);
    }];
    
    UILabel *labPSum = [[UILabel alloc] init];
    labPSum.text = @"昨日消费人数";
    labPSum.textColor = [UIColor blackColor];
    labPSum.font = [UIFont boldSystemFontOfSize:15.0];
    [view1 addSubview:labPSum];
    [labPSum makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(labPeople.centerX);
        make.top.equalTo(labPeople.bottom).offset(8);
    }];
    
    UILabel *labMoney = [[UILabel alloc] init];
    labMoney.text = @"20.06万";
    labMoney.textColor = [UIColor redColor];
    labMoney.font = [UIFont systemFontOfSize:20.0];
    [view1 addSubview:labMoney];
    [labMoney makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line.right).offset(50);
        make.top.equalTo(view1.top).offset(8);
    }];
    
    UILabel *labPJinE = [[UILabel alloc] init];
    labPJinE.text = @"昨日成交金额";
    labPJinE.textColor = [UIColor blackColor];
    labPJinE.font = [UIFont boldSystemFontOfSize:15.0];
    [view1 addSubview:labPJinE];
    [labPJinE makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(labMoney.centerX);
        make.top.equalTo(labMoney.bottom).offset(8);
    }];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, view1.frame.origin.y+view1.frame.size.height+5, ScreenWidth, 30)];
    view2.backgroundColor = [UIColor whiteColor];
    [headView addSubview:view2];
    
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"公告-(2)"]];
    [view2 addSubview:image];
    [image makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view2.left).offset(16);
        make.centerY.equalTo(view2.centerY);
        make.width.height.equalTo(14);
    }];
    
    UILabel *labGG = [[UILabel alloc] init];
    labGG.text = @"用户昵称";
    labGG.textColor = [UIColor redColor];
    labGG.font = [UIFont systemFontOfSize:13.0];
    [view2 addSubview:labGG];
    [labGG makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(image.centerY);
        make.left.equalTo(image.right).offset(8);
    }];
    
    UILabel *labText = [[UILabel alloc] init];
    labText.text = @"成功抢的iPhone8手机一部，Mac电脑一台，i-Watch一块";
    labText.font = [UIFont systemFontOfSize:13.0];
    [view2 addSubview:labText];
    [labText makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(image.centerY);
        make.left.equalTo(labGG.right).offset(8);
        make.right.equalTo(view2.right).offset(-8);
        
    }];
    
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(0, view2.frame.origin.y+view2.frame.size.height+5, ScreenWidth, 90)];
    view3.backgroundColor = [UIColor whiteColor];
    [headView addSubview:view3];
    
    UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mszc"]];
    [view3 addSubview:image2];
    [image2 makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view3.top).offset(8);
        make.left.equalTo(view3.left).offset(16);
    }];
    
    UILabel *labMS = [[UILabel alloc] init];
    labMS.text = @"限时秒杀，先到先得";
    labMS.textColor = [UIColor darkGrayColor];
    labMS.textAlignment = NSTextAlignmentRight;
    labMS.font = [UIFont systemFontOfSize:13.0];
    [view3 addSubview:labMS];
    [labMS makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(image2.centerY);
        make.right.equalTo(view3.right).offset(-16);
    }];
    
    UILabel *lab3L = [[UILabel alloc] init];
    lab3L.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [view3 addSubview:lab3L];
    [lab3L makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(image2.bottom).offset(8);
        make.right.equalTo(view3.right);
        make.left.equalTo(view3.left);
        make.height.equalTo(1);
    }];
    
    UILabel *time1 = [[UILabel alloc] init];
    time1.text = @"10:00";
    time1.textColor = [UIColor redColor];
    time1.font = [UIFont systemFontOfSize:18.0];
    [view3 addSubview:time1];
    [time1 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view3.left).offset(16);
        make.top.equalTo(lab3L.bottom).offset(8);
    }];
    
    UILabel *time1D = [[UILabel alloc] init];
    time1D.text = @"秒杀中";
    time1D.textColor = [UIColor redColor];
    time1D.font = [UIFont systemFontOfSize:12.0];
    [view3 addSubview:time1D];
    [time1D makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(time1.centerX);
        make.top.equalTo(time1.bottom).offset(4);
    }];
    
    UILabel *time2 = [[UILabel alloc] init];
    time2.text = @"11:00";
    time2.textColor = [UIColor redColor];
    time2.font = [UIFont systemFontOfSize:18.0];
    [view3 addSubview:time2];
    [time2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(time1.right).offset(16);
        make.top.equalTo(lab3L.bottom).offset(8);
    }];
    
    UILabel *time2D = [[UILabel alloc] init];
    time2D.text = @"秒杀中";
    time2D.textColor = [UIColor redColor];
    time2D.font = [UIFont systemFontOfSize:12.0];
    [view3 addSubview:time2D];
    [time2D makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(time2.centerX);
        make.top.equalTo(time2.bottom).offset(4);
    }];
    
    
}
#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    MainCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    TimeView *views = [[[NSBundle mainBundle] loadNibNamed:@"TimeView" owner:nil options:nil] lastObject];
    views.frame = CGRectMake(0, 0, ScreenWidth, 55);
    
    return views;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 55.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CheXianViewController *clbx = [[CheXianViewController alloc] init];
    [self.navigationController pushViewController:clbx animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
